local UIBase = require("view.UIBase")
local NearPlayerView = class("NearPlayerView", UIBase)

function NearPlayerView:ctor(sceneId, targetId)
    self._sceneId = sceneId
    self._targetId = targetId
    local resource = {
        path = "Prefabs/View/NearView",
        name = "NearPlayerView",
        canvas = 2
    }
    NearPlayerView.super.ctor(self, resource)
end

function NearPlayerView:start()
    self._listView:SetTemplate(
        handler(self, self._onItemUpdate),
        handler(self, self._onValueChange),
        handler(self, self._onItemTouch)
    )
    self._btnClose.onClick:AddListener(handler(self, self._onBtnCloseClick))
    G_UserData:getBase():c2sCheckNearPlayers(self._sceneId)
    self._signalNearPlayer = G_SignalManager:add(SignalConst.EVENT_NEAR_PLAYERS, handler(self, self._onEventNearPlayer))
end

function NearPlayerView:onDestroy()
    self._signalNearPlayer:remove()
    self._signalNearPlayer = nil
end

function NearPlayerView:_onItemUpdate(item, index)
    local starIndex = index + 1
    item:updateUI(self._users[starIndex])
end

function NearPlayerView:_onValueChange()
end

function NearPlayerView:_onItemTouch()
end

function NearPlayerView:_onBtnCloseClick()
    self:close()
end

function NearPlayerView:_onEventNearPlayer(id, seneId, users)
    self._users = users
    self._listView:ReSize(#users)
end

return NearPlayerView
